/**
 * 
 */
package com.gxt.utils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

/**
 * @author JasonGuo
 * @Description:
 */
public class FFMpegTest {
	private String ffmpegEXE;
	
	public FFMpegTest(String ffmpegEXE) {
		super();
		this.ffmpegEXE = ffmpegEXE;
	}
	
	public void convertor(String videoInputPath, String videoOutputPath) throws IOException {
//		$ ffmpeg -i input.mp4 output.avi
		List<String> command = new ArrayList<>();
		command.add(ffmpegEXE);
		command.add("-i");
		command.add(videoInputPath);
		command.add(videoOutputPath);
		
		for(String a : command) {
			System.out.println(a);
		}
		
		ProcessBuilder builder = new ProcessBuilder(command);
		Process process = builder.start();
		
//		由于做处理时会产生临时文件 占用线程
//		为防止资源浪费进行处理
		
		InputStream errorStream = process.getErrorStream();
		InputStreamReader inputStreamReader = new InputStreamReader(errorStream);
		BufferedReader br = new BufferedReader(inputStreamReader);
//		对br作处理 解决上述问题
		
		String line = ""; //每一行读取出来
		while((line = br.readLine()) != null) {
//			读到null才停
		}
		if(br != null) {
			br.close();
		}
		
		if(inputStreamReader != null) {
			inputStreamReader.close();
		}
		
		if(errorStream != null) {
			errorStream.close();
		}
	}
	public static void main(String[] args) {
		FFMpegTest ffMpegTest = new FFMpegTest("E:\\ffmpeg\\bin\\ffmpeg.exe");
		try {
			ffMpegTest.convertor("E:\\gxt_videos\\180425CFA4RB6T0H\\video\\test.mp4", 
					"E:\\\\gxt_videos\\\\180425CFA4RB6T0H\\\\video\\\\test.avi");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

}
